<?php
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

Loader::loadUserClass('MemberController', false, 'admin');

class Admin_IndexController extends MemberController
{
    public function indexAction() {
        $menus = get_menu_cache($_SESSION[RlModel::GROUPID]);
        $index = 0;
        $submenu_html = '';
        
        foreach($menus as $k => $v) {
            if($v['notshow']) {
                unset($menus[$k]);
            }
        }
        
        foreach($menus as $menu) {
            foreach($menu['submenus'] as $k => $v) {
                if($v['notshow']) {
                    unset($menu['submenus'][$k]);
                }
            }
            
            if(!empty($menu['submenus'])) {
                $submenu_html .= get_submenus($menu['id'], $menu['submenus'], $index != 0, true);
                $index++;
            }
        }

        $this->_view->assign(array(
            'head_title' => $this->_lang['admin_index'],
            'body_style' => 'index_body',
            'menus' => $menus,
            'submenu_html' => $submenu_html
        ));
        
        $this->_view->display('index');
    }
    
    public function mainAction() {
        $this->_view->display('index_main');
    }
    
    public function addrAction() {
        $addr = '-';
        $ip = get_cookie('a_ip');
        if(is_ip($ip)) {
            $addr = file_get_contents('http://whois.pconline.com.cn/ip.jsp?ip='.$ip);
            $addr = iconv('gb2312', 'utf-8', $addr);
        }
        
        exit($addr);
    }
    
    public function qqloginAction() {
        $member = $this->getAdmin();
        
        $this->_view->assign('member', $member);
        $this->_view->display('index_qq_login');
    }
}

function get_menu_cache($groupid) {
    $menupath = 'menu_group_' . $groupid;
    $menus = cache_read($menupath);
    if(!$menus) {
        $menudb = Loader::loadUserModel('MenusModel', true, 'admin');
        $twomenu = get_menus($menudb, $groupid);
        $menus = $twomenu['menus'];
    }

    return $menus;
}

function get_submenus($pid, $menus, $hidden = false, $istop = false) {
    $html = '<ul id="container_'.$pid.'" '.($istop ? ' name="container"' : '').' class="filetree treeview"'.($hidden ? ' style="display:none;"' : '').'>';
    $len = count($menus);
    $index = 0;
    foreach($menus as $menu) {
        $html .= '<li class="closed '.($menu['isdir'] == 1 ? 'expandable' : '').($index == $len -1 ? ' last' : '').'" style="width:92%;">';
        $html .= '<div id="flag_'.$menu['id'].'" class="'.($menu['isdir'] == 1 ? 'hitarea' : '').' closed-hitarea expandable-hitarea" onclick="javascript:showMenu('.$menu['id'].')"></div>';
        $html .= $menu['isdir'] == 1 ? '<span class="folder"><a href="javascript:showMenu('.$menu['id'].')">'.$menu['menu_name'].'</a></span>' : '<span class="file"><a href="'.ADMIN_NAME.'?m=admin&c='.$menu['c'].'&a='.$menu['a'].'" target="right">'.$menu['menu_name'].'</a></span>';
        foreach($menu['submenus'] as $k => $v) {
            if($v['notshow']) {
                unset($menu['submenus'][$k]);
            }
        }
        
        if(!empty($menu['submenus'])) {
            $html .= get_submenus($menu['id'], $menu['submenus'], true);
        }
        $html .= '</li>';
        $index++;
    }
    $html .= '</ul>';
    
    return $html;
}